//
//  LoopImage+Extension .swift
//  LoopWeibo
//
//  Created by loop安 on 2016/12/15.
//  Copyright © 2016年 loop. All rights reserved.
//

import UIKit

extension UIImage{

    //声明成类方法,可以直接使用
    class func snapShotCurrentWindow()->UIImage{
        
        let window = UIApplication.shared.keyWindow
        //开启图片上下文
        UIGraphicsBeginImageContextWithOptions(window!.frame.size, false, 0)
        //将当前window绘制到图形上下文中
        window?.drawHierarchy(in: window!.frame, afterScreenUpdates: true)
        //从图形上下文中拿到图片
        let image = UIGraphicsGetImageFromCurrentImageContext()
        //关闭图形上下文
        UIGraphicsEndImageContext()
        
        return image!
    }
    
    
    func scaleImage(width:CGFloat)->UIImage{
        let imageWidth = self.size.width
        let imageHeight = self.size.height
    
        if imageWidth < width {
            return self
        }
        
        let scaleHeight = imageHeight / imageWidth * width
        let imageBounds = CGRect(x: 0, y: 0, width: width, height: scaleHeight)
        
        UIGraphicsBeginImageContextWithOptions(imageBounds.size, false, 0)
        
        self.draw(in: imageBounds)
        
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
    
        return image!
    }



}


